/**
 * 积分商城-今日任务
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'

import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'
import {
    HttpService,
    getLocalTime,
    Tips,
    dateFormat
} from '../Tools'

export default class MyOrder extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataList: [],
            dataLoad: 1,
            dataNull: 0,
            dataEnd: 0
        }
        this.page = 1;
        this.pagesize = 10;
        this.toBottom = 100;
    }

    componentWillMount() {
        this.getData()
    }

    componentDidMount() {
        window.onscroll = () => {
            const {
                dataEnd,
                dataNull,
                page
            } = this.state;
            if (this.dataLoad == 1 || dataEnd == 1 || dataNull == 1)
                return;
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            if (document.body.clientHeight - window.innerHeight - scrollTop <= this.toBottom) {
                this.page++;
                this.getData();
            }
        }
    }
    //获取数据列表
    getData(status) {
        this.dataLoad = 1
        this.setState({
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        });
        if (status == 1)
            this.page = 1
        HttpService.query({
            url: '/src/Data/myorder.json',
            // url: '/jf/order/pageOrder',
            data: {
                token: sessionStorage.appToken,
                page: this.page,
                size: this.pagesize
            },
            success: (res => {
                this.dataLoad = 0
                this.setState({
                    dataLoad: 0
                });
                if (res.code == 10140) {
                    //获取数据成功
                    let dataList = res.data.list
                    if (status != 1)
                        dataList = this.state.dataList.concat(res.data.list);
                    this.setState({
                        dataList
                    });
                    if (res.data.total == this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                    } else if (res.data.list.length < this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                        if (dataList.length == 0) {
                            this.setState({
                                dataEnd: 0,
                                dataNull: 1
                            });
                        }
                    }
                    return;
                }
                Tips(res.msg);
            })
        })
    }
    render() {
        const {
            dataLoad,
            dataNull,
            dataEnd,
            dataList
        } = this.state
        let dataLists = dataList.map(function(item) {
            const time = dateFormat(new Date(item.orderCreateTime), '-');
            return (
                <Link className='item' to={'/orderdetail/'+item.id}>
                    <p>订单编号：{item.orderNo}</p>
                    {item.orderStatus==1?<p>快递单号：{item.deliverNo}</p>:null}
                    <p>下单时间：{time}</p>
                    <p>订单状态：<span className={item.orderStatus==2?'c-gary':'c-blue'}>{item.orderStatus==2?'待发货':'已发货'}</span></p>
                    <p>商品名称：{item.goodName}</p>
                    <p>收货人：{item.receiveName}</p>
                </Link>
            )
        })
        return (
            <div className="task_today">
                <Header title="我的订单" />
                <div className='itgmyorder'>
                    {dataList.length>0?<div className='intro'>备注：可通过以下订单号在百度中查询物流状态</div>:null}
                    {dataLists}
                    <LoadingBox dataLoad={dataLoad} dataNull={dataNull} dataEnd={dataEnd} dataNullMsg='未查询到订单'  />
                </div>
            </div>
        )
    }
}
